package com.lvy.hczn.front.business.config;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lvy.hczn.front.business.common.CacheConstants;
import com.lvy.hczn.front.business.service.ConvertDataService;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.constant.RedisConstants;
import com.lvy.hczn.front.common.core.redis.RedisCache;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.common.utils.spring.SpringUtils;
import com.lvy.hczn.front.system.domain.business.Warehouse;
import com.lvy.hczn.front.system.service.WarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.math.BigDecimal;
import java.math.BigInteger;

@Configuration
@Order(1)
@Slf4j
public class InitConfig {

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ConvertDataService convertDataService;

    /**
     * 初始化仓库时，需根据区编码和运营区域编码生成基本仓库信息
     * 因为目前设计为：以一个区域为topic，区域下的总仓和前置仓在区域topic下进行的数据监听
     * 即：一个区域topic，负责区域内所有仓库信息交互，
     *
     * @param
     * @return void
     * @author yw
     * @date 2023-05-28 18:24:51
     */
    @Bean
    public void initWarehouseCheck() {
        redisCache.del(RedisConstants.WAREHOUSE_WMS);
        Warehouse warehouse = warehouseService.selectLocalWarehouseFront();
        if (warehouse != null) {
            redisCache.set(RedisConstants.WAREHOUSE_WMS, warehouse);
        }
        redisCache.setCacheObject(CacheConstants.SORT_TASK_DOWNLOADING, false);
        redisCache.set(RedisConstants.WCS_SEND_DELAY, "1");
        redisCache.set(CacheConstants.WCS_RESPONSE + "weight:out", 5);
        redisCache.set(RedisConstants.WCS_WEIGHT_UNIT_COEFFICIENT, new BigDecimal(BigInteger.ONE));
        redisCache.set(RedisConstants.BG_TIME_START, 8);
        redisCache.set(RedisConstants.BG_TIME_END, 19);
        warehouseService.initBaseData(null);
        ThreadUtil.execAsync(() -> {
            convertDataService.converttList();
        });
    }
}
